package com.bac.bacplatform;

import java.util.HashMap;
import java.util.Map;

import com.android.volley.Response;
import com.android.volley.Response.Listener;
import com.bac.utils.Config;
import com.bac.utils.Util;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class LoginPwdActivity extends BaseActivity implements OnClickListener{
	private EditText mEt_phone;
	private Button mBtn_getcode;
	private String username_str,phone_str;
	private int countdown = 60;
	private Handler mHandler = new Handler();// 全局handler
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.loginpassword_back);
	}

	@Override
	public void init() {
		mEt_phone = (EditText) findViewById(R.id.phone_hint);
		mBtn_getcode = (Button) findViewById(R.id.regist_verify_btn);
		mBtn_getcode.setOnClickListener(this);

		
		mEt_phone.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
			}

			@Override
			public void afterTextChanged(Editable arg0) {
				if (mEt_phone.getText().toString().length() > 0 && countdown == 60) {
					mBtn_getcode.setEnabled(true);
					mBtn_getcode.setBackgroundResource(R.drawable.red_btn);
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.regist_verify_btn:
			//验证手机号和用户名，成功后发送验证码
			if (verifyInput(3)) {
				getCode();
				mBtn_getcode.setEnabled(false);
				mBtn_getcode.setBackgroundResource(R.drawable.white_corner);
				new Thread(new ClassCut()).start();//倒计时
			} else {
				return;
			}
			
			break;

		default:
			break;
		}
		
	}
	//数据验证
	private boolean verifyInput(int i) {
		if (mEt_phone.getText().toString().length() == 0) {
			showToast(R.string.phone_none);
			return false;
		} else {
			return true;
		}
	}
	
	class ClassCut implements Runnable {
		@Override
		public void run() {
			while (countdown > 0) {
				countdown--;
				mHandler.post(new Runnable() {
					@Override
					public void run() {

						mBtn_getcode.setText("剩余:" + countdown + "\"");
					}
				});
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			mHandler.post(new Runnable() {
				@Override
				public void run() {
					mBtn_getcode.setText(R.string.regist_helpverify);
					mBtn_getcode.setEnabled(true);
					mBtn_getcode.setBackgroundResource(R.drawable.red_btn);
				}
			});
			countdown = 60;
		}
	}
	
	//验证用户名和手机号，返回验证码
	private void getCode(){
		/**
		 * 成功返回结果
		 */
		Listener<String> sL = new Response.Listener<String>() {
			@Override
			public void onResponse(String response) {
				dismissLoadingProgress();
				Log.i("TAG----success", response.toString());
				showToast("验证成功");
				Intent intent = new Intent(LoginPwdActivity.this,ResetLoginPwdActivity.class);
				intent.putExtra("id",response);
				startActivity(intent);
			}
		};
		
//		username_str = mEt_username.getText().toString().trim();
		phone_str = mEt_phone.getText().toString().trim();
		if (phone_str.length() <= 0) {
			showToast(R.string.phone_none);
			return;
		}
		showLoadingProgress();
		String path = Config.URL_RESET_PWD;
		Map<String, String> map = new HashMap<String, String>();
//		map.put("username",username_str);
		map.put("phone",phone_str);
		Util.httpRequestToGetWithParams(path, sL, this, map);
	}

}





















